﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using MediaPortal.GUI.Library;

namespace MyNetflix2
{
    public class RSS : Queue
    { 
         /// <summary>
        /// <summary>
        /// Episodes
        /// </summary>
        /// <param name="user"></param>
        public RSS(User user)
            : base(user)
        {}

        #region Locals


        private const string RSSTop100 = @"http://rss.netflix.com/Top100RSS";
        private const string RSSNewReleases = @"http://rss.netflix.com/NewReleasesRSS";
        private const string RSSTop25Action = @"http://rss.netflix.com/Top25RSS?gid=296";
        private const string RSSTop25Animae = @"http://rss.netflix.com/Top25RSS?gid=623";
        private const string RSSTop25Bluray = @"http://rss.netflix.com/Top25RSS?gid=2444";
        private const string RSSTop25Children = @"http://rss.netflix.com/Top25RSS?gid=302";
        private const string RSSTop25Classics = @"http://rss.netflix.com/Top25RSS?gid=306";
        private const string RSSTop25Comedy = @"http://rss.netflix.com/Top25RSS?gid=307";
        private const string RSSTop25Documentary = @"http://rss.netflix.com/Top25RSS?gid=864";
        private const string RSSTop25Drama = @"http://rss.netflix.com/Top25RSS?gid=315";
        private const string RSSTop25Faith = @"http://rss.netflix.com/Top25RSS?gid=2108";
        private const string RSSTop25Foreign = @"http://rss.netflix.com/Top25RSS?gid=2514";
        private const string RSSTop25Gay = @"http://rss.netflix.com/Top25RSS?gid=330";
        private const string RSSTop25Horror = @"http://rss.netflix.com/Top25RSS?gid=338";
        private const string RSSTop25Independent = @"http://rss.netflix.com/Top25RSS?gid=343";
        private const string RSSTop25Music = @"http://rss.netflix.com/Top25RSS?gid=2310";
        private const string RSSTop25Romance = @"http://rss.netflix.com/Top25RSS?gid=371";
        private const string RSSTop25Sci = @"http://rss.netflix.com/Top25RSS?gid=373";
        private const string RSSTop25Special = @"http://rss.netflix.com/Top25RSS?gid=2223";
        private const string RSSTop25Sports = @"http://rss.netflix.com/Top25RSS?gid=2190";
        private const string RSSTop25Television = @"http://rss.netflix.com/Top25RSS?gid=2197";
        private const string RSSTop25Thrillers = @"http://rss.netflix.com/Top25RSS?gid=387";

        #endregion

        /// <summary>
        /// Get the Top x from Netflix RSS Feeds
        /// </summary>
        /// <param name="iGenre">An int of the Genre to return</param>
        /// <returns></returns>
        public override List<ODataItem> GetTop(int iGenre)
        {
            string sURL = "";
            List<ODataItem> lInfo = new List<ODataItem>();

            #region Get Correct Feed

            //------------------------------------------------------------------------------
            // Figure out which Genre was picked
            //------------------------------------------------------------------------------
            switch (iGenre)
            {
                case -2:
                    sURL = RSSNewReleases;
                    break;
                case -1:
                    sURL = RSSTop100;
                    break;
                case 1:
                    sURL = RSSTop25Action;
                    break;
                case 2:
                    sURL = RSSTop25Animae;
                    break;
                case 3:
                    sURL = RSSTop25Bluray;
                    break;
                case 4:
                    sURL = RSSTop25Children;
                    break;
                case 5:
                    sURL = RSSTop25Classics;
                    break;
                case 6:
                    sURL = RSSTop25Comedy;
                    break;
                case 7:
                    sURL = RSSTop25Documentary;
                    break;
                case 8:
                    sURL = RSSTop25Drama;
                    break;
                case 9:
                    sURL = RSSTop25Faith;
                    break;
                case 10:
                    sURL = RSSTop25Foreign;
                    break;
                case 11:
                    sURL = RSSTop25Gay;
                    break;
                case 12:
                    sURL = RSSTop25Horror;
                    break;
                case 13:
                    sURL = RSSTop25Independent;
                    break;
                case 14:
                    sURL = RSSTop25Music;
                    break;
                case 15:
                    sURL = RSSTop25Romance;
                    break;
                case 16:
                    sURL = RSSTop25Sci;
                    break;
                case 17:
                    sURL = RSSTop25Special;
                    break;
                case 18:
                    sURL = RSSTop25Sports;
                    break;
                case 19:
                    sURL = RSSTop25Television;
                    break;
                case 20:
                    sURL = RSSTop25Thrillers;
                    break;
            }

            #endregion

            //------------------------------------------------------------------------------
            // Parse to linq
            //------------------------------------------------------------------------------
            XElement root = XElement.Load(sURL);
 
            //------------------------------------------------------------------------------
            // Now stuff all the goodies in a ODataItem object and write it to a List
            //------------------------------------------------------------------------------
            try
            {
                foreach (XElement node in root.Descendants("item"))
                {
                    ODataItem info = new ODataItem();
                    info.Title = node.Element("title").Value;
                    info.Synopsis = Regex.Replace(node.Element("description").Value, "<[^>]*>", string.Empty);
                    info.CoverId = node.Element("link").Value.Remove(0, node.Element("link").Value.LastIndexOf('/') + 1);
                    info.GetCoverImage("http://cdn-0.nflximg.com/us/boxshots/large/" + info.CoverId + ".jpg");
                    Thread getBackdrops = new Thread(info.GetBackgroundImage);
                    getBackdrops.Start();
                    lInfo.Add(info);
                }

                return lInfo;

            }
            catch (Exception ex)
            {
                Log.Error("My NetFlix - RSS:GetTop: Error - {0}", ex.Message);
                return null;
            }
        }
    }
}
